package com.qf.servlet;

import com.qf.pojo.ResponseJson;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceIpml;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            //校验验证码.先获取验证码
        String usercode = request.getParameter("usercode");
        //先判断.验证码是否正确
        //从服务器获取四位数的验证码
        String  checkcode_server = (String) request.getSession().getAttribute("CHECKCODE_SERVER");
        if (!usercode.equalsIgnoreCase(checkcode_server)){
            //如果不相等,则重新输入验证码
            request.setAttribute("msg","验证码输入错误,请重新输入");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);

        }
        //验证码相等,获取username
        String username = request.getParameter("username");
        //调用业务层,查询用户名
        UserService userService = new UserServiceIpml();
        Boolean flag = userService.findUserByName(username);
        //进行判断
        if (flag){
            response.sendRedirect(request.getContextPath()+"/jsp/index.jsp");
        }else{
            request.setAttribute("msg","用户不存在,请重新登录");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request,response);
    }
}
